﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MakeTableTool
{
    /// <summary>
    /// 每行数据
    /// </summary>
    public class TableRow
    {
        //Col < - >FieldInfo 
        public List<TableField> tableFields;
        //Col < - > dataObject
        public List<object> dataValue = new List<object>();

        //Key列表
        private List<int> keys = new List<int>();

        public TableRow(List<TableField> tableFields)
        {
            this.tableFields = tableFields;
        }

        public ulong Key
        {
            get
            {
                return CombineKey(keys);
            }
        }
        public void AddData(object value)
        {
            var index = dataValue.Count;
            var define = tableFields[index].define;
            var fieldType = tableFields[index].fieldType;


            if (define == TableField.TableDefine.Key)
            {
                if (value == null)
                {
                    throw new Exception("Key :不能为Null");
                }

                int i_value;
                var strValue = value.ToString();
                if (int.TryParse(strValue, out i_value))
                {
                    keys.Add(i_value);
                }

            }

            ///对于没有填的数值给一个默认值
            if (value == null && tableFields[index].isList == false)
            {
                switch (fieldType)
                {
                    case TableField.TableBaseType.BYTE:
                    case TableField.TableBaseType.SHORT:
                    case TableField.TableBaseType.INT:
                    case TableField.TableBaseType.FLOAT:
                    case TableField.TableBaseType.ULONG:
                        value = 0;
                        break;
                    case TableField.TableBaseType.BOOL:
                        value = false;
                        break;
                    case TableField.TableBaseType.STRING:
                        value = "";
                        break;
                  
                }
            }
            dataValue.Add(value);
        }

        /// <summary>
        /// 合并Key值
        /// </summary>
        /// <param name="keys"></param>
        /// <returns></returns>
        private ulong CombineKey(List<int> keys)
        {
            var keyNum = keys.Count;
            var key = (ulong)0;
            if (keyNum == 1)
            {
                key = (ulong)keys[0];
            }
            else if (keyNum == 2)
            {
                var key1 = keys[0];
                var key2 = keys[1];
                key = (((ulong)key1 & 0xffffffff) | (((ulong)key2 & 0xffffffff) << 32));
            }
            else if (keyNum == 3)
            {
                var key1 = keys[0];
                var key2 = keys[1];
                var key3 = keys[2];
                key = (((ulong)key1 & 0xffffffff) | (((ulong)key2 & 0xffff) << 32) | (((ulong)key3 & 0xffff) << 48));
            }
            else if (keyNum == 4)
            {
                var key1 = keys[0];
                var key2 = keys[1];
                var key3 = keys[2];
                var key4 = keys[3];
                key = (((ulong)key1 & 0xffff) | (((ulong)key2 & 0xffff) << 16) | (((ulong)key3 & 0xffff) << 32) | (((ulong)key4 & 0xffff) << 48));
            }

            return key;
        }

        public string LogData()
        {
            string logStr = "rowData :";

            for (int i = 0; i < tableFields.Count; i++)
            {
                string colLog = String.Format("Col {0},Type : {1},Data:{2}", i, tableFields[i].fieldType.ToString(), dataValue[i] == null ? "" : dataValue[i].ToString());
                logStr += colLog + "\n";
            }

            return logStr;
        }

    }
}
